$(document).ready(function() {

        var formToSubmit = '#add_attribute_on_group';
        var formbutton = '#newAttributeButton';
        var dialogClass = 'add_view_attributes_on_group';

        validate_select_attributes = function ()
        {
            //FORM VALIDATION
            jQuery(formToSubmit).validate({
                    rules: {
                        /*newObjectDialog_object_type: { check_item_dropdown: true },
                        name: 'required' */
                    }, 
                    highlight: function(element) {
                        $(element).parent().parent().addClass("error");
                    },
                    unhighlight: function(element) {
                        $(element).parent().parent().removeClass("error");
                    },
                    submitHandler: function(form) {

                        //disable add button                            
                        $(formbutton).attr("disabled","disabled");

                        $(formToSubmit+' .status_info').html(loader_html());

                        var form = $(formToSubmit);
                        var form_data = form.serialize();

                        $.ajax({
                                url: form.attr('action'),
                                dataType: 'json',
                                type: "POST",
                                data: form_data,
                                success: function (data, textStatus, xhr)
                                {

                                    if(data.action==true){

                                        $(formToSubmit+' .status_info').html(alert_html(data.msg,'success')); 

                                        function action_delay(){
                                            $('.'+dialogClass).dialog("close");  
                                        }

                                        // Fill form  with updated fileds
                                        reload_group_form(myObject['groupId'],myObject['groupReference'])

                                        window.setTimeout(function() { action_delay(); }, 1001);

                                    }else{

                                        $(formbutton).removeAttr("disabled");
                                        $(formToSubmit+' .status_info').html(alert_html(data.msg,'error'));
                                        $(formToSubmit).animate({
                                                scrollTop:  $(formToSubmit+' .status_info').offset().top
                                            }, 200);
                                    }
                                },
                                error: function (xhr, textStatus, errorThrown) {
                                    //enable add button                            
                                    $(formbutton).removeAttr("disabled");
                                    $(formToSubmit+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                }
                        });
                    },
                    errorPlacement: function(error, element){}
            })
        }

        add_view_attributes_on_group = function (group_id,elem)
        {
            var url = base_url+'group_attributes/view_add_attribute_on_group/'+group_id;
            var dialogTitle = 'Select attributes';
            var buttons = [
                {
                    id: 'newAttributeButton',
                    text: "Add",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];

            var DialogOnOpen = validate_select_attributes;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,548,440)
        }

        $('.view_add_attributes_on_group').live('click',function(){
                group_id = $(this).attr('data-holder');
                if(group_id=='')
                    {
                    alert('Select type first')
                }else
                    {
                    myObject['groupReference'] = this;
                    myObject['groupId'] = group_id;
                    add_view_attributes_on_group(group_id,null);  
                }
        })
})